<template>
  <div>
<!-- 导航   -->
    <div class="menu1" :class="{isHide:isHide}" >
      <div style="flex: 2" >
        <div class="logo">个人博客</div>
      </div>
      <div class="menu_class" style="flex: 6" >
        <ul>
          <li>
            <a href="/home">
              <div style="display: flex;align-items: center;text-align: center">
                <img width="30" height="30" src="../assets/img/afr.png"/>
                <span style="margin-left: 10px">首页</span>
              </div>
            </a>
          </li>
          <li>
            <a href="/record">
              <div style="display: flex;align-items: center;text-align: center">
                <img width="30" height="30" src="../assets/img/bigbon.png"/>
                <span style="margin-left: 10px">记录</span>
              </div>
            </a>
          </li>
          <li>
            <a href="/share">
              <div style="display: flex;align-items: center;text-align: center">
                <img width="30" height="30" src="../assets/img/share.png"/>
                <span style="margin-left: 10px">分享</span>
              </div>
            </a>
          </li>
          <li>
            <a href="/about">
              <div style="display: flex;align-items: center;text-align: center">
                <img width="30" height="30" src="../assets/img/fushishan.png"/>
                <span style="margin-left: 10px">关于</span>
              </div>
            </a>
          </li>
          <li>
            <a href="/story">
              <div style="display: flex;align-items: center;text-align: center">
                <img width="30" height="30" src="../assets/img/lundun.png"/>
                <span style="margin-left: 10px">故事</span>
              </div>
            </a>
          </li>
        </ul>
      </div>
      <div v-if="user.username" style="flex: 1;">
        <el-dropdown placement="bottom">
          <div class="tx">
            <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png\n'" width="50px" height="50px" style="border-radius: 50%;"/>
            <div style="margin-top: 16px">{{user.name}}</div>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="$router.push('/person')">个人信息</el-dropdown-item>
            <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
<!-- 显示页面   -->
    <div style="width: 100%;min-height: 80vh;background-color: #FFFFFF;">
      <div>
        <router-view @update:user="updateUser"/>
      </div>
    </div>



  </div>
</template>

<script>
import MyFooter from "@/components/myFooter.vue";

export default {
  name: "Layout.vue",
  components: {MyFooter},
  data(){
    return{
      user: JSON.parse(localStorage.getItem("news") || "{}") ,
      img: '../assets/img/img01.jpg',
      oldScrollTop: 0,
      isHide: false,
      flag: false,
      input:''
    }
  },
  mounted() {
    // 监听页面滚动事件
    window.addEventListener("scroll", this.scrolling)
  },
  methods: {
    // 头部导航做鼠标滚动优化
    scrolling() {
      // 滚动条距文档顶部的距离（做兼容处理）===》不懂的可以结合画图理清逻辑
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop ||
          document.body.scrollTop
      // 滚动条滚动的距离
      let scrollStep = scrollTop - this.oldScrollTop;
      // 更新——滚动前，滚动条距文档顶部的距离
      this.oldScrollTop = scrollTop;
      if (scrollStep < 0) {
        this.isHide = false;
      } else {
        this.isHide = true;
      }
    },
    logout(){
      localStorage.removeItem('news')  // 清除当前的token和用户数据
      this.$router.push('/login')
    },

    updateUser(user) {   // 获取子组件传过来的数据  更新当前页面的数据
      this.user = JSON.parse(JSON.stringify(user))  // 让父级的对象跟子级的对象毫无关联
    },
  }

}
</script>

<style scoped>
.menu_class{
  font-size: 20px;
}
.menu1.isHide {
  top: -104px;
}
.menu1{
  display: flex;
  text-align: center;
  background-color:rgba(255,255,255,1);
  height: 60px;
  width: 100%;
  position: sticky;
  left: 0;
  top: 0;
  z-index: 100000;
  transition: all 0.3s;
}

.logo{
  font-family: "仓耳章鱼小丸子体 W01";
  font-size: 33px;
  margin: 10px 0px;
}
a{
  text-decoration: none;
  color: black;
}
.menu_class ul{
  display: flex;
  list-style-type: none;
  justify-content: center;
}
.menu_class li{
  flex: 1;
  margin: 0 20px;
  font-family: "仓耳章鱼小丸子体 W01";

}
.menu_class li>a:hover{
  color: #42b983;
  text-decoration: turquoise;
}
.tx{
  display: flex;
  justify-content: center;
  text-align: center;
  margin-top: 2px;
  font-family: "仓耳章鱼小丸子体 W01";
}
</style>

